//
//  ShopVIewController.m
//  PetApp
//
//  Created by apple on 16/3/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShopVIewController.h"
#import "ListOfShop.h"
#import "NetworkingHandle.h"
#import "HeaderOfShop.h"
#import "ModelOfShop.h"
#import "CellOfShop.h"
#import "ShopOfSelectVC.h"

@interface ShopVIewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataArr;

@property (nonatomic, retain) UIImageView *imageViewOfNight;

@end

@implementation ShopVIewController

- (void)dealloc {
    [_imageViewOfNight release];
    [_tableView release];
    [_dataArr release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createNaviController];
    [self createSubViews];
    [self handleData];
}

#pragma mark - 处理数据
- (void)handleData {
    
    [NetworkingHandle handleWorkData:@"http://ts.leepet.com/plugins/API.v1.0/?&a=goods&m=indexTopicGoods" AndBody:@"page=1&pageSize=10" value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
        
        NSArray *tempArr = [[result objectForKey:@"data"] objectForKey:@"info"];
        self.dataArr = [NSMutableArray array];
        for (NSDictionary *dic in tempArr) {
            ModelOfShop *model = [[ModelOfShop alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:model];
            [model release];
            
        }
        [self.tableView reloadData];
    }];
}


- (void)createSubViews {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellOfShop class] forCellReuseIdentifier:@"poolOfCell"];
    [self createTableViewOfHeaderView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}
#pragma mark - Cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfShop *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfCell"];
    ModelOfShop *model = [self.dataArr objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0];
    [cell handleDataWithModel:model];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}
#pragma mark - 返回高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.dataArr.count - 1){
        return 140;
    }else{
        return 150;
    }
    
}
#pragma mark - 点击弹出详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ShopOfSelectVC *shopSelectVC = [[ShopOfSelectVC alloc] init];
    shopSelectVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:shopSelectVC animated:YES];
    [shopSelectVC release];
    shopSelectVC.topicId = [(ModelOfShop *)self.dataArr[indexPath.row] topic_id];
    shopSelectVC.title = [self.dataArr[indexPath.row] title];
    
    
}

#pragma mark - 创建头部
- (void)createTableViewOfHeaderView {
    HeaderOfShop *shopView = [[HeaderOfShop alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 185)];
    self.tableView.tableHeaderView = shopView;
    [shopView release];
}

#pragma mark - 创建NaviController的内容
- (void)createNaviController {
    /** 设置左侧button属性 */
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 40, 20);
    [leftButton setTitle:@"分类" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [leftButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
    leftButton.tag = 100;
    
    /** 设置右侧button属性 */
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 25, 25);
    [rightButton setBackgroundImage:[UIImage imageNamed:@"dianhua"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
    rightButton.tag = 101;
    
    
    self.navigationItem.title = @"乐购商城";
    
}
#pragma mark - 左右naviItem的点击效果
- (void)handleAction:(UIButton *)button {
    if (button.tag == 100) {
        ListOfShop *shopListVC = [[ListOfShop alloc] init];
        shopListVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:shopListVC animated:YES];
        [shopListVC release];
        
    }else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"客服服务时间为09:00——18:00" preferredStyle:UIAlertControllerStyleActionSheet];
        
        // 添加action
        UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"400-040-7278" style:0 handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4000407278"]];
        }];
        
        UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:1 handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [alert addAction:actionOK];
        [alert addAction:actionCancel];
        [self presentViewController:alert animated:YES completion:nil];
    
    }


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
